//
//  FHEmoticonKeyBoard.swift
//  WeiBo
//
//  Created by qfh on 16/7/19.
//  Copyright © 2016年 qiufuhe. All rights reserved.
//

import UIKit

//  重用标记
private let FHEmoticonCollectionViewCellIdentifier = "FHEmoticonCollectionViewCellIdentifier"

class FHEmoticonKeyBoard: UIView {

    /// 底部视图
    lazy private var toolBar: FHEmoticonToolBar = FHEmoticonToolBar(frame: CGRectZero)
    
    private lazy var emoticonCollectionView: UICollectionView = {
    
        let flowLayout = UICollectionViewFlowLayout()
        
        flowLayout.minimumLineSpacing = 0
        
        flowLayout.minimumInteritemSpacing = 0
        
        flowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        let view = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        
        view.bounces = false
        
        view.pagingEnabled = true
        
        view.showsVerticalScrollIndicator = false
        
        view.showsHorizontalScrollIndicator = false

    
        view.delegate = self
        view.dataSource = self
        
        view.registerClass(FHEmoticonCollectionViewCell.self, forCellWithReuseIdentifier: FHEmoticonCollectionViewCellIdentifier)
        
    
        view.backgroundColor = self.backgroundColor
        
        
        return view
    
    
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUi()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUi() {
    
        addSubview(toolBar)
        
        addSubview(emoticonCollectionView)
        
        
        toolBar.callBack = { (type: CZEmoticonToolBarButtonType) in
            
            
            let indexPath: NSIndexPath
            
            
            switch type {
            
            case .Normal:
                
                
                indexPath = NSIndexPath(forItem: 0, inSection: 0)
                
            case .Emoji:
                
                indexPath = NSIndexPath(forItem: 0, inSection: 1)


                
            case .Lxh:
                
                
                indexPath = NSIndexPath(forItem: 0, inSection: 2)
        
            
            }
            
            
            self.emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: [], animated: false)
            
        
        
        }
        
        
        toolBar.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(self)
            
            make.trailing.equalTo(self)
            
            make.bottom.equalTo(self)
            
            make.height.equalTo(30)
            
            
        }
        
        
        emoticonCollectionView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            
            make.trailing.equalTo(self)
            
            make.leading.equalTo(self)
            
            make.bottom.equalTo(toolBar.snp_top)
            
        }
        
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        
        let flowLayout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        
        
        let itemWidth = emoticonCollectionView.width
        
        let itemHeight = emoticonCollectionView.height
        
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemHeight)
        
        
    }
    

}

/// MARK: - UICollectionViewDelegate, UICollectionViewDataSource
extension FHEmoticonKeyBoard: UICollectionViewDelegate, UICollectionViewDataSource {


    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return FHEmoticonTools.sharedEmoticonTools.allEmoticonArray[section].count
    }
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return FHEmoticonTools.sharedEmoticonTools.allEmoticonArray.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("FHEmoticonCollectionViewCellIdentifier", forIndexPath: indexPath) as! FHEmoticonCollectionViewCell
        
        cell.emoticon = FHEmoticonTools.sharedEmoticonTools.allEmoticonArray[indexPath.section][indexPath.item]
                
        return cell
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        
        let cells = emoticonCollectionView.visibleCells().sort { (firstCell, secnodCell) -> Bool in
            
            return firstCell.x < secnodCell.x
        }
        
        if cells.count == 2 {
        
            let cell: UICollectionViewCell
            
            let firstCell = cells.first!
            
            let seconedCell = cells.last!
        
        
        
            let firstCellContentOffSetX = abs(firstCell.x - scrollView.contentOffset.x)
            
            let secondCellContentOffSetX = seconedCell.x - scrollView.contentOffset.x
            
            if firstCellContentOffSetX < secondCellContentOffSetX {
            
                cell = firstCell
            } else {
            
            
                cell = seconedCell
            }
        
            let indePath = emoticonCollectionView.indexPathForCell(cell)
            
            
            let section = indePath?.section
            
            
            toolBar.selectedButtonWidthSection(section!)
        
        
        
        
        }
        
        
        
    }
    
    
    
    
    
    



}



